package hovn.app.quickpay.activity;

import hovn.app.quickpay.R;
import hovn.app.quickpay.activity.pay.AlipayFukuan;
import hovn.app.quickpay.activity.pay.AlipayMingpian;
import hovn.app.quickpay.activity.pay.AlipayScan;
import hovn.app.quickpay.activity.pay.AlipayShoukuan;
import hovn.app.quickpay.activity.pay.WechatFukuan;
import hovn.app.quickpay.activity.pay.WechatMingpian;
import hovn.app.quickpay.activity.pay.WechatScan;
import hovn.app.quickpay.activity.pay.WechatShoukuan;
import hovn.app.quickpay.util.RootShell;

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_layout);
		// showGithub();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_hide_icon:
			// hide
			new AlertDialog.Builder(this).setTitle("警告").setMessage("隐藏后将不能再打开应用及快捷方式，但桌面部件和磁贴还可正常使用。")
					.setPositiveButton("确定隐藏", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							hideAppIcon();
						}
					}).setNegativeButton("我再想想", null).show();

			return true;
		case R.id.action_get_root:
			// root
			getSu();
			return true;

		case R.id.action_about_app:
			AlertDialog.Builder ab = new AlertDialog.Builder(this);
			ab.setTitle("感谢使用本软件").setMessage(R.string.aboutapp).setNegativeButton("捐赠原作者", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					donateViaAlipay2();
				}
			}).setPositiveButton("捐赠我", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					donateViaAlipay1();
				}
			});
			AlertDialog alert = ab.create();

			alert.show();

			return true;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void donateViaAlipay1() {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		String qrurl = "alipayqr://platformapi/startapp?saId=10000007&qrcode=https://qr.alipay.com/apgz2obuuhm2tnzo6f";// 零下300度
		intent.setData(Uri.parse(qrurl));

		try {
			startActivity(intent);
		} catch (Throwable t) {
			Toast.makeText(this, "未安装支付宝APP", Toast.LENGTH_SHORT).show();
		}
	}

	private void donateViaAlipay2() {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		String qrurl = "alipayqr://platformapi/startapp?saId=10000007&qrcode=https://qr.alipay.com/apqiqql0hgh5pmv54d";// 原作者
		intent.setData(Uri.parse(qrurl));

		try {
			startActivity(intent);
		} catch (Throwable t) {
			Toast.makeText(this, "未安装支付宝APP", Toast.LENGTH_SHORT).show();
		}
	}

	private void getSu() {
		RootShell rootShell = RootShell.open();
		rootShell.execute("su");
		rootShell.close();
	}

	private void hideAppIcon() {
		getPackageManager().setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
				PackageManager.DONT_KILL_APP);

	}

	@SuppressWarnings("unused")
	private void showAppIcon() {
		getPackageManager().setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
				PackageManager.DONT_KILL_APP);

	}



	public void zfbsys(View v) {
		startActivity(new Intent(this, AlipayScan.class));
	}

	public void zfbfkm(View v) {
		startActivity(new Intent(this, AlipayFukuan.class));
	}

	public void zfbskm(View v) {
		startActivity(new Intent(this, AlipayShoukuan.class));
	}

	public void zfbme(View v) {
		startActivity(new Intent(this, AlipayMingpian.class));
	}

	public void wxsys(View v) {
		startActivity(new Intent(this, WechatScan.class));
	}

	public void wxfkm(View v) {
		startActivity(new Intent(this, WechatFukuan.class));
	}

	public void wxskm(View v) {
		startActivity(new Intent(this, WechatShoukuan.class));
	}

	public void wxme(View v) {
		startActivity(new Intent(this, WechatMingpian.class));
	}

}
